package com.kool.kmetrics.metrics.prometheus;

import com.alibaba.fastjson.JSON;
import com.kool.kmetrics.consumer.vo.ClientNoAckSendPacketCnt;
import com.kool.kmetrics.consumer.vo.NoAckSendPacketLog;
import com.kool.kmetrics.metrics.MetricsComputer;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Gauge;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author luyu
 */
@Component
public class NoAckSendPacketProm implements MetricsComputer {
    @Resource
    private Gauge noAckSendPacketGauge;


    @Bean
    public Gauge noAckSendPacketGauge() {
        return Gauge.build("no_ack_send_packet_gauge", "no ack send packet gauge")
                .labelNames("clientId")
                .register(CollectorRegistry.defaultRegistry);
    }

    @Override
    public void compute(String log) {
        NoAckSendPacketLog noAckSendPacketLog = JSON.parseObject(log, NoAckSendPacketLog.class);
        List<ClientNoAckSendPacketCnt> clientNoAckSendPacketCnts = noAckSendPacketLog.getClientNoAckSendPacketCnts();
        if (clientNoAckSendPacketCnts != null) {
            for (ClientNoAckSendPacketCnt clientNoAckSendPacketCnt : clientNoAckSendPacketCnts) {
                String clientId = clientNoAckSendPacketCnt.getClientId();
                int cnt = clientNoAckSendPacketCnt.getCnt();
                noAckSendPacketGauge.labels(clientId).set(cnt);
            }
        }
    }
}
